package com.ysd.lis.controller.qc;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.dto.qc.ExportExcel;
import com.ysd.lis.request.QcResultTimeParam;
import com.ysd.lis.request.qc.QcPageDto;
import com.ysd.lis.request.qc.QcResultAddParam;
import com.ysd.lis.request.qc.QcResultParam;
import com.ysd.lis.service.qc.QcMaterialItemResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 员工表
 * 前端控制器
 * </p>
 *
 * @author pengrongshuai
 * @since 2023-12-26
 */
@Api(tags = "质控结果")
@RestController
@RequestMapping("/api/qc/qcMaterialItemResult")
public class QcMaterialItemResultController extends BaseController {

    @Autowired
    QcMaterialItemResultService qcMaterialItemResultService;

    @GetMapping("/getQcMaterialItemResultData")
    public Result getQcMaterialItemResultData(QcResultTimeParam qcResultTimeParam) {
        return qcMaterialItemResultService.getQcMaterialItemResultData(qcResultTimeParam);
    }

    @PostMapping("/addQcMaterialItemResult")
    public Result addQcMaterialItemResult(@RequestBody QcResultAddParam qcResultAddParam) {
        return qcMaterialItemResultService.addQcMaterialItemResult(qcResultAddParam);
    }

    @ApiOperation("根据质控物质控项目按月查询质控结果")
    @GetMapping("/getQcResultByMonth")
    public Result getQcResultByMonth(QcPageDto dto) {
        return qcMaterialItemResultService.getQcResultByMonth(dto);
    }

    @ApiOperation("批量新增或修改质控结果（按项目）")
    @PostMapping("/saveOrUpdateMaterialResult")
    public Result saveOrUpdateMaterialResult(@RequestBody QcResultParam param) {
        return qcMaterialItemResultService.saveOrUpdateMaterialResult(param);
    }

    @ApiOperation("获取质控物汇总信息")
    @GetMapping("/getIQCMaterialsInfoAsync")
    public Result getIQCMaterialsInfoAsync(QcPageDto dto) {
        return qcMaterialItemResultService.getIQCMaterialsInfoAsync(dto);
    }

    @PostMapping("/transToQcResultByMainInfo")
    public Result transToQcResultByMainInfo(@RequestBody QcResultAddParam qcResultAddParam) {
        return qcMaterialItemResultService.transToQcResultByMainInfo(qcResultAddParam);
    }

    @PostMapping("/transToQcResultByRptResultId")
    public Result transToQcResultByRptResultId(@RequestBody QcResultAddParam qcResultAddParam) {
        return qcMaterialItemResultService.transToQcResultByRptResultId(qcResultAddParam);
    }

    @GetMapping("/getQcResultListByQcMaterial")
    public Result getQcResultListByQcMaterial(QcResultTimeParam qcResultTimeParam) {
        return qcMaterialItemResultService.getQcResultListByQcMaterial(qcResultTimeParam);
    }

    @ApiOperation("批量删除")
    @PostMapping("/batchDeleteMaterialItemResult")
    public Result batchDeleteMaterialItemResult(@RequestBody QcResultParam dto) {
        return qcMaterialItemResultService.batchDeleteMaterialItemResult(dto);
    }


    @ApiOperation("根据仪器，质控编号，项目查询月质控汇数据")
    @GetMapping("/findQcMaterialMonthData")
    public Result findQcMaterialMonthData(QcResultParam dto) {
        return qcMaterialItemResultService.findQcMaterialMonthData(dto);
    }

    @ApiOperation("质控月报表导出excel")
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody ExportExcel exportExcel, HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        ClassPathResource classPathResource = new ClassPathResource("template/质控月报表.xlsx");

        InputStream inputStream = classPathResource.getInputStream();
        ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).excelType(ExcelTypeEnum.XLSX).autoCloseStream(false).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        FillConfig fillconfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).forceNewRow(Boolean.TRUE).build();
        Map<String, Object> stringObjectHashMap = new HashMap<>();

        stringObjectHashMap.put("list", exportExcel.getData());
        stringObjectHashMap.put("month", exportExcel.getQcMonthName());
        stringObjectHashMap.put("bathCode", exportExcel.getBatchNum());
        stringObjectHashMap.put("insterCode", exportExcel.getInstrName());
        stringObjectHashMap.put("summary", exportExcel.getSummary());
        excelWriter.fill(stringObjectHashMap, writeSheet);
        excelWriter.fill(new FillWrapper("list", exportExcel.getData()), fillconfig, writeSheet);
        excelWriter.finish();
    }
}
